<?php

/**
 * Created by JetBrains PhpStorm.
 * User: root
 * Date: 10/9/12
 * Time: 12:58 AM
 * To change this template use File | Settings | File Templates.
 */
class Setup_Router extends Setup_Abstract implements Setup_Interface
{

    private $routes;

    public function __construct()
    {
        $this->routes = Config::get('routes') && is_array(Config::get('routes')) && count(Config::get('routes')) > 0 ? Config::get('routes') : array();
        
    }

    public function setup()
    {
        
        Config::set('app.template', Config::get('default_template'));

        Config::set('app.router', Router_Abstract::getRouter(Config::get('default_router')));

        $sections = Config::get('sections');

        if ($sections)
        {
            foreach ($sections as $section => $sectionValue)
            {
                
                if (Util::appSection(FALSE) === $section)
                {

                    Config::set('app.template', $sectionValue['template']);

                    Config::set('app.router', Router_Abstract::getRouter($sectionValue['router']));
                     
                    if (!empty($sectionValue['routes']))
                    {
                    
                        $this->routes = $sectionValue['routes'];
                    }
                    else
                    {
                        $this->routes = array();
                    }
                }
            }
        }
        ;
        $this->setRoutes();

    }

    public function setRoutes()
    {

        foreach ($this->routes as $routerOptions)
        {

            Config::get('app.router')->addRoute(new Router_Route($routerOptions));
        }

        $this->loadModuleRoutes();

    }

    public function loadModuleRoutes()
    {

        $dirObj = new fDirectory(Config::get('app.module_path'));

        $dirObj = $dirObj->scan();

        foreach ($dirObj as $dir)
        {
            if (file_exists($dir->getPath() . 'Config.php'))
            {
          
                $pathArray = explode('/', trim($dir->getPath(), '/'));

                $moduleName = $pathArray[count($pathArray) - 1];

                if (class_exists($moduleName .Util::appSection(). '_Config'))
                {
                    $moduleRoutes = call_user_func($moduleName . '_Config::getRoutes');

                    foreach ($moduleRoutes as $route)
                    {
                        Config::get('app.router')->addRoute(new Router_Route($route));
                    }
                }
            }

            $section = Util::appSection();

            if (file_exists($dir->getPath() . ucfirst($section) . '/Config.php'))
            {
                $pathArray = explode('/', trim($dir->getPath(), '/'));

                $moduleName = $pathArray[count($pathArray) - 1];

                if (class_exists($moduleName . $section . '_Config'))
                {
                    $moduleRoutes = call_user_func($moduleName . $section . '_Config::getRoutes');

                    if (Util::checkArray($moduleRoutes))
                    {
                        foreach ($moduleRoutes as $route)
                        {
                            Config::get('app.router')->addRoute(new Router_Route($route));
                        }
                    }
                }
            }
        }

    }

}